<?php
/* 
 * name        : Internal » Sphider Search
 * description : Search using Sphider
 * context     : page
 * instructions: 
 * 
 */

$default_config = array(
	'cat_id'     => '',
	'assign'     => false,
	'no_results' => 'There were no results for your search.  Please try another.',
	'endpoint'   => '',
);

$config = array_merge($default_config, parse_config($config));


$params = array(
	'query'    => $_REQUEST['q'],
	'category' => $config['cat_id'],
	'search'   => 1,
	'start'    => isset($_REQUEST['page']) ? $_REQUEST['page'] : 1
);


$result = json_decode(file_get_contents($config['endpoint'] . '?' . http_build_query($params)));

if ($config['assign']) {
	$smarty->assign($config['assign'], $result);
	return;
}

?>

<div id="jrank-results">
	
	<div class="metadata"> 
		<span class="time"><?=$result->time?> seconds</span>
		<span class="count"><?if($result->total_results>0):?><?=$result->from?> &ndash; <?=$result->to?> of <?=$result->total_results?><?else:?>0<?endif;?> results</span>
		<span class="words">for <strong><?=$result->query?></strong></span>
	</div>
	
	<? if ($result->total_results == 0): ?>
	<p><?=$config['no_results']?></p>
	<? endif; ?>
	
	<? foreach ($result->qry_results as $entry): ?>
	<dl> 
		<dt><strong><?=$entry->num?>.</strong> [<?=$entry->weight?>%] <a href="<?=$entry->url?>"><?=$entry->title?></a></dt>
		<dd class="description"><?=$entry->fulltxt?></dd>
		<dd class="url"><?=$entry->url?> [<?=$entry->page_size?>]</dd>
	</dl>
	<? endforeach; ?>
	
	<div class="pagination"> 
		<? if ($result->prev): ?>
		<a href="?q=<?=urlencode($result->query)?>&amp;page=<?=$result->prev?>">&laquo; prev</a>
		<? else: ?>
		<span class="disabled">&laquo; prev</span> 
		<? endif; ?>
		
		<?=$result->start?> of <?=$result->pages?>
		
		<? if ($result->start < $result->pages): ?>
		<a href="?q=<?=urlencode($result->query)?>&amp;page=<?=$result->next?>">next &raquo;</a>
		<? else: ?>
		<span class="disabled">next &raquo;</span>
		<? endif; ?>
	</div>
	
</div>
